from google.appengine.ext import webapp

class EventHook(object):

    def __init__(self):
        self.__handlers = []
 
    def __iadd__(self, handler):
        self.__handlers.append(handler)
        return self
 
    def __isub__(self, handler):
        self.__handlers.remove(handler)
        return self
 
    def fire(self, *args, **keywargs):
        for handler in self.__handlers:
            handler(*args, **keywargs)
 
    def clearObjectHandlers(self, inObject):
        for theHandler in self.__handlers:
            if theHandler.im_self == inObject:
                self -= theHandler

class EventfulHandler(webapp.RequestHandler):    
    
    OnRequest = EventHook()
    
    def getx(self,*items):
        
        self.response.out.write(self.OnRequest==EventfulHandler.OnRequest)
        if items:
            self._oldget(items)
        else:
            self._oldget()
        
    def __init__(self, *args, **kwargs):
        super(EventfulHandler, self).__init__(*args, **kwargs)
        
